package org.cordova.plugin.jfyun;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.w3c.dom.Text;

import com.svw.cmdb.R;

import java.util.Timer;
import java.util.TimerTask;

public class JfyunDialog extends Activity {

  private Context context;

  private String action;
  private JSONArray args;
  private ImageView imgView;
  private SpeechRecognizer mIat;
  private SharedPreferences mSharedPreferences;
  private TextView txtView,txtErr;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xfyun_dialog);
    this.context=this.getApplicationContext();
    //btnTest=(Button)findViewById(R.id.edit);
    this.imgView=(ImageView)findViewById(R.id.imageView);
    this.imgView.setImageResource(R.drawable.voice_empty);
    //findViewById(R.id.i)
    this.txtView=(TextView)findViewById(R.id.txtView);
    this.txtErr=(TextView)findViewById(R.id.txtErr);
    mSharedPreferences = getSharedPreferences("com.iflytek.setting",
      Activity.MODE_PRIVATE);
    this.startListen();
  }

  private void startListen(){
   try{

      // 1.创建SpeechRecognizer对象，第二个参数：本地听写时传InitListener
     mIat = SpeechRecognizer.createRecognizer(context, null);
      // 2.设置听写参数，详见《科大讯飞MSC API手册(Android)》SpeechConstant类
      mIat.setParameter(SpeechConstant.DOMAIN, "iat");
      // 设置语言
     mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
      mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
	  
	  mIat.setParameter(SpeechConstant.ASR_PTT, "0");//不现实标点符号

//     mIat.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("iat_vadbos_preference", "2000"));
//
//     // 设置语音后端点:后端点静音检测时间，即用户停止说话多长时间内即认为不再输入， 自动停止录音
//     mIat.setParameter(SpeechConstant.VAD_EOS, mSharedPreferences.getString("iat_vadeos_preference", "500"));

      // 3.开始听写
      mIat.startListening(myRecolistener);



    }catch(Exception ex){
     Jfyun.callbackContext.error(ex.toString());
    }
  }


  private RecognizerListener myRecolistener=new RecognizerListener(){
    private String resultWords;

    @Override
    public void onBeginOfSpeech() {
      // TODO Auto-generated method stub
      this.resultWords="";
      txtErr.setText("请讲话...");
    }

    // 结束录音
    @Override
    public void onEndOfSpeech() {
      // TODO Auto-generated method stub

      txtErr.setText("识别中...");
    }

    @Override
    public void onError(SpeechError arg0) {
      // TODO Auto-generated method stub
      imgView.setImageResource(R.drawable.warning);

	  if(arg0.getErrorCode()==20006){
		  txtErr.setText("请先开启麦克风权限!");
	  }else{
		  txtErr.setText("未知错误!");
	  }
      

      Timer timer = new Timer();
      timer.schedule(new TimerTask() {
        @Override
        public void run() {
          //do something
          finish();
        }
      },1000);//延时1s执行

    }

    // 扩展用接口
    @Override
    public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
      // TODO Auto-generated method stub

    }

    @Override
    public void onResult(RecognizerResult result, boolean isLast) {
      // TODO Auto-generated method stub
      String str= JsonParser.parseIatResult(result.getResultString());
      //Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
      if(isLast){
        this.resultWords+=str;
        Jfyun.callbackContext.success(this.resultWords);
        finish();
      }else{
        this.resultWords+=str;
      }
    }

    // 音量值0~30
    @Override
    public void onVolumeChanged(int volume, byte[] arg1) {
      // TODO Auto-generated method stub
      //txtView.setText("音量:"+volume);
      if(volume<10){
        imgView.setImageResource(R.drawable.voice_4);
      }else if(volume>=10 && volume<15){
        imgView.setImageResource(R.drawable.voice_3);
      }else if(volume>=15 && volume<20){
        imgView.setImageResource(R.drawable.voice_2);
      }else if(volume>=20 && volume<25){
        imgView.setImageResource(R.drawable.voice_1);
      }else{
        imgView.setImageResource(R.drawable.voice_full);
      }
    }
  };


}
